package com.jwthhealth_pub.old.constitution.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.constitution.ConstitutionTypeUtil;
import com.jwthhealth_pub.old.constitution.view.ConstitutionAnswerActivity;

/**
 * Created by Administrator on 2016/12/15.
 */
public class ConstitutionAnswerAdatper extends RecyclerView.Adapter {
    private Context context;
    private int type;
    private String describe;
    private float score;

    //用于计算得分位置
    private LinearLayout scoreLayout;
    private View scoreSeekBar;
    private float percent;

    public View getScoreSeekBar() {
        return scoreSeekBar;
    }

    public LinearLayout getScoreLayout() {
        return scoreLayout;
    }

    public ConstitutionAnswerAdatper(ConstitutionAnswerActivity constitutionAnswerActivity, String describe, String score, int type) {
        this.context = constitutionAnswerActivity;
        this.type = type;
        this.describe = describe
        ;
        this.score = Float.parseFloat(score);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_YTPE.ITEM_HEAD.ordinal()) {
            return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.item_answer_head, parent, false));
        } else {
            return new BodyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_answer_body, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            HeadViewHolder headViewHolder = (HeadViewHolder) holder;
            // TODO: 2016/12/15 类型，结果
            String text = ConstitutionTypeUtil.getType(type);
            headViewHolder.itemType.setText(text);
            headViewHolder.itemResponse.setText(describe);
//            headViewHolder.seekBar.setProgress((int)score);
            percent = score/100f;
            //计算分数出现位置

            this.scoreSeekBar = headViewHolder.seekBar;
            this.scoreLayout = headViewHolder.scoreLayout;

        } else {
            BodyViewHolder bodyViewHolder = (BodyViewHolder) holder;
            String answerTitle = ConstitutionTypeUtil.getAnswerTitle(position);
            bodyViewHolder.img.setText(answerTitle.substring(0, 1));
            if (position == 4 || position == 2 || position == 5 || position == 7 || position == 8) {
                bodyViewHolder.img.setBackground(context.getResources().getDrawable(R.drawable.btn_circle_bluegreen_bg));
            } else {
                bodyViewHolder.img.setBackground(context.getResources().getDrawable(R.drawable.btn_circle_purple_bg));
            }
            bodyViewHolder.title.setText(answerTitle);
            bodyViewHolder.summary.setText(ConstitutionTypeUtil.getAnswerSummary(type, position));
        }
    }

    @Override
    public int getItemCount() {
        return 9;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_YTPE.ITEM_HEAD.ordinal();
        } else {
            return ITEM_YTPE.ITEM_BODY.ordinal();
        }
    }

    /**
     * 计算分数显示位置
     * @param width
     */
    public void putScorePos(int width) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        float marginLeft = width * percent;
        params.setMargins((int)marginLeft, 0, 0, 0);
        TextView textView = new TextView(context);
        float result = (float) (Math.round(score * 100)) / 100;
        textView.setText(String.valueOf(result));
        textView.setTextSize(12);
        textView.setGravity(Gravity.CENTER);
        textView.setBackground(context.getResources().getDrawable(R.mipmap.score_bg));
        textView.setLayoutParams(params);
        scoreLayout.addView(textView);
    }

    class HeadViewHolder extends RecyclerView.ViewHolder{

        TextView itemResponse, itemType;
        LinearLayout scoreLayout;
        private View seekBar;

        public HeadViewHolder(View itemView) {
            super(itemView);
            itemResponse = (TextView) itemView.findViewById(R.id.tv_response);
            itemType = (TextView) itemView.findViewById(R.id.tv_type);
            scoreLayout = (LinearLayout) itemView.findViewById(R.id.layour_score);
            seekBar =  itemView.findViewById(R.id.item_seekbar);
//            seekBar.setEnabled(false);
//            seekBar.setSecondaryProgress(context.getResources().getColor(R.color.primarypurple));
            seekBar.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return true;
                }
            });
        }

    }

    class BodyViewHolder extends RecyclerView.ViewHolder{
        TextView img, title, summary;

        public BodyViewHolder(View itemView) {
            super(itemView);
            img = (TextView) itemView.findViewById(R.id.item_answer_bg);
            title = (TextView) itemView.findViewById(R.id.item_answer_title);
            summary = (TextView) itemView.findViewById(R.id.item_answer_summary);
        }
    }

    private enum ITEM_YTPE{
        ITEM_HEAD, ITEM_BODY;
    }
}
